package com.itheima.entry.dao.impl;

import com.itheima.entry.dao.IStudentDAO;
import com.itheima.entry.dao.SaveStudent;
import com.itheima.entry.domain.Student;
import com.itheima.entry.factory.StudentFactroy;

import com.itheima.util.ExportExcel;
import com.itheima.util.POIUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.T;

import java.io.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class StudentDaoImpl extends StudentFactroy implements IStudentDAO {

    private static ArrayList<Student> listStudent = new ArrayList<>();

//        静态代码块,加入伪信息用来测试
//    static {
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d");
//        ArrayList<String> strList = new ArrayList<>(List.of(
//                "s0001,张兴,男,29,1999/9/1,1,14559855987",
//                "s0002,金节,女,28,1992/9/1,1,14780325259",
//                "s0003,魏出,男,24,1996/9/8,1,14990991810",
//                "s0004,沈山,男,18,2003/5/19,1,13643930317",
//                "s0005,东郭悠悠,女,35,1985/12/5,1,16720006256",
//                "s0006,何扩优,男,28,1992/12/10,2,15800232940",
//                "s0007,吴三,女,27,1994/7/24,2,17258282870",
//                "s0008,尤香,女,34,1987/2/11,2,15688466261",
//                "s0009,夏侯仁,男,40,1980/11/8,2,17230330065",
//                "s0010,金伙,女,22,1999/1/10,2,13091600931",
//                "s0011,魏先,女,25,1996/5/8,3,15653057423",
//                "s0012,蒋电,女,34,1987/1/15,3,15679047139",
//                "s0013,吴历,男,34,1986/8/24,3,13172467073",
//                "s0014,金行四,女,22,1999/1/5,3,17857077501",
//                "s0015,楮尔叶,女,40,1981/5/26,3,17545122524",
//                "s0016,孔己,女,28,1993/7/20,4,15007665325",
//                "s0017,陈贞,男,30,1991/7/1,4,18171954509",
//                "s0018,沈寸飞,女,41,1980/6/24,4,15601473648",
//                "s0019,吕全申,男,20,2000/11/28,4,13484300801",
//                "s0020,楮艺,男,38,1983/7/16,4,15015281699",
//                "s0021,沈太博,男,40,1981/3/2,5,15104533272",
//                "s0022,赵舟似,女,41,1980/4/9,5,18954167123",
//                "s0023,赵仆冈,女,29,1992/6/26,5,17333653352",
//                "s0024,许功奇,男,27,1994/1/18,5,15334044300",
//                "s0025,蒋师师,女,21,2000/1/15,5,19940533172",
//                "s0026,钱弓且,女,25,1996/1/12,6,15740763231",
//                "s0027,孙鼎,男,38,1982/12/17,6,13953426409",
//                "s0028,李丘,男,30,1991/4/19,6,13826086140",
//                "s0029,周慧,女,22,1999/3/20,6,13034694938",
//                "s0030,何匀句,女,22,1998/12/27,6,17385263570",
//                "s0031,韩下旧,男,24,1996/12/24,7,14582955701",
//                "s0032,赵师,女,37,1984/3/22,7,15667058289",
//                "s0033,韩黛穆,女,37,1984/3/5,7,18302113327",
//                "s0034,施予,女,36,1984/9/6,7,18305809977",
//                "s0035,卫吃仙,女,33,1988/7/27,7,15223105480",
//                "s0036,沈以燕,女,39,1982/6/12,8,17306571582",
//                "s0037,孔世佳,男,23,1997/12/23,8,15103154740",
//                "s0038,陈奴儿,男,19,2002/2/12,8,14587139243",
//                "s0039,曹寸,女,35,1986/3/7,8,17795195538",
//                "s0040,孙早早,女,22,1999/3/25,8,19856538847",
//                "s0041,郑厂,女,29,1992/7/16,9,17544463502",
//                "s0042,何诗舟,女,27,1993/10/27,9,13814913303",
//                "s0043,孔九九,女,39,1982/1/29,9,15614294183",
//                "s0044,秦父伏,女,31,1990/1/28,9,13972086409",
//                "s0045,吴目,男,36,1985/3/24,9,15585391891",
//                "s0046,张甄,女,32,1989/2/27,10,15915478613",
//                "s0047,何母,女,23,1998/8/1,10,14923454674",
//                "s0048,张匹,男,28,1992/8/28,10,16610291873",
//                "s0049,卫贺,男,29,1992/2/12,10,15347175417",
//                "s0050,冯圣,女,37,1983/12/29,10,16670134401",
//                "s0051,许微,女,27,1994/2/26,11,18310511457",
//                "s0052,钱七七,女,36,1984/9/1,11,17862953007",
//                "s0053,魏付,女,37,1983/11/11,11,14964210676",
//                "s0054,钱毕,女,18,2003/8/3,11,13411892846",
//                "s0055,上官煜,男,22,1999/3/24,11,13123066437",
//                "s0056,陈许媛,女,18,2002/10/21,12,19828685339",
//                "s0057,钱合,男,33,1988/1/1,12,16671701444",
//                "s0058,朱思诗,男,18,2003/8/5,12,15818397027",
//                "s0059,吕仔,男,21,2000/2/1,12,19902755411",
//                "s0060,张延芳,女,34,1986/8/17,12,18354061226",
//                "s0061,周长历,男,24,1997/5/20,13,17691751134",
//                "s0062,严仁,男,26,1995/3/20,13,15018136302",
//                "s0063,蒋石婧,女,22,1998/9/3,13,18828541487",
//                "s0064,冯忆,女,32,1988/9/7,13,17574537347",
//                "s0065,秦末,男,19,2002/4/13,13,18073049838",
//                "s0066,王郎,男,22,1999/4/23,14,15140965190",
//                "s0067,曹凤,女,20,2001/4/9,14,13089812728",
//                "s0068,何云,女,38,1983/6/27,14,14793227317",
//                "s0069,李头,女,33,1987/9/23,14,15531485988",
//                "s0070,魏仙,女,38,1982/9/27,14,13674713313",
//                "s0071,蒋丈,女,30,1991/5/20,15,13786194195",
//                "s0072,魏仅,男,23,1998/2/23,15,18763580917",
//                "s0073,杨寸侃,男,33,1988/6/14,15,17582344422",
//                "s0074,蒋不悔,男,32,1989/7/3,15,15168336847",
//                "s0075,楮雄星,女,19,2002/4/14,15,19990208978",
//                "s0076,金贝贝,女,19,2002/4/14,16,18933620329",
//                "s0077,赵斥,女,25,1995/10/16,16,13554758503",
//                "s0078,司马乔,女,33,1987/10/6,16,19897732340",
//                "s0079,王肌,男,34,1986/10/14,16,18364054632",
//                "s0080,华巴,男,27,1994/3/15,16,14729142761",
//                "s0081,楮贺对,女,18,2002/9/19,17,13943207750",
//                "s0082,王岁,女,22,1999/3/19,17,13940276754",
//                "s0083,朱巴戒,男,39,1982/4/10,17,13342881786",
//                "s0084,吕户臣,女,24,1997/4/6,17,18049488841",
//                "s0085,李茂贞,女,29,1992/6/29,17,18811472071",
//                "s0086,蒋夫礼,男,21,2000/5/30,18,15806667122",
//                "s0087,蒋式邂,女,39,1982/8/10,18,15066630261",
//                "s0088,慕容禾辽,女,28,1993/2/8,18,13385718234",
//                "s0089,吴节操,男,31,1989/12/18,18,17809849428",
//                "s0090,陈认包,男,23,1997/10/4,18,15907879562",
//                "s0091,王智人,女,35,1986/3/4,19,18753878715",
//                "s0092,韩去病,女,39,1982/4/23,19,17634533225",
//                "s0093,金儿,女,37,1984/3/6,19,19812805158",
//                "s0094,王九,女,38,1982/10/28,19,13443228965",
//                "s0095,卫国,女,17,2003/12/28,19,17357927206",
//                "s0096,许钰,男,28,1993/4/29,20,19948139457",
//                "s0097,杨贺幼,男,35,1986/4/8,20,17778803145",
//                "s0098,朱宁,男,34,1986/9/17,20,18102601675",
//                "s0099,孔九儿,女,38,1982/12/30,20,13731972664"
//
//        ));
//
//        //学号 0,名字 1,性别 2,生日 4,组号 5,电话 6
//        for (String strInfo : strList) {
//            Student st = new Student();
//            String[] info = strInfo.split(",");
//            st.setId(info[0]);
//            st.setName(info[1]);
//            st.setGender(info[2]);
//            st.setBirthday(LocalDate.parse(info[4], formatter));
//            st.setGroup(Integer.parseInt(info[5]));
//            st.setPhoneNum(info[6]);
//            listStudent.add(st);
//        }
//
//        try {
//            //实时保存用户信息
//            Thread saveStudent = new Thread(new SaveStudent(listStudent));
//            saveStudent.start();
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//
//    }


    //    静态代码块,启动就从本地文件读取信息到程序中,并开启另一个线程实时储存信息到本地
    static {
        ObjectInputStream obReader = null;
        ArrayList<Student> list = null;
        try {
            obReader = new ObjectInputStream(new FileInputStream("D:\\heima_system\\backup\\studentList.txt"));
            list = (ArrayList<Student>) obReader.readObject();

            listStudent = list;

        } catch (ClassNotFoundException | IOException e) {
//            e.printStackTrace();
            System.out.println("学生系统暂无数据暂无数据");
        } finally {

            if (obReader != null) {
                try {
                    obReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        try {
            //实时保存用户信息
            Thread saveStudent = new Thread(new SaveStudent(listStudent));
            saveStudent.start();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void add(Student st) {
        listStudent.add(st);
    }

    @Override
    public void remove(String id) {
        int index = getIndex(id);
        listStudent.remove(index);


    }

    @Override
    public void update(Student st) {
        int index = getIndex(st.getId());
        listStudent.set(index, st);
    }

    @Override
    //通过id获得学生对象索引
    public int getIndex(String id) {
        for (int i = 0; i < listStudent.size(); i++) {
            Student student = listStudent.get(i);
            if (student != null && student.getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }


    @Override
    public ArrayList<Student> allStudents() {
        return listStudent;

    }

    @Override
    public ArrayList<Student> sameNameStudents(String name) {
        ArrayList<Student> result = new ArrayList<>();
        for (Student student : listStudent) {
            if (name.equals(student.getName())) {
                result.add(student);
            }
        }
        return result;

    }

    @Override
    public ArrayList<Student> phoneToStudent(String phone) {
        ArrayList<Student> result = new ArrayList<>();
        for (Student student : listStudent) {
            if (phone.equals(student.getPhoneNum())) {
                result.add(student);
            }
        }
        return result;
    }


    //导出所有学生信息
    @Override
    public boolean exportInfo(String username) {
        if (listStudent == null || listStudent.size() == 0) {
            return false;
        }
        exportAllStudent("学生信息");//调用导出方法
        return true;
    }

    //表头是 "序号", "学号", "姓名", "性别", "年龄", "生日", "手机号码", "组名"的导出
    //String titleInfo是表格的标题
    private void exportAllStudent(String titleInfo) {
        HSSFWorkbook workbook = new HSSFWorkbook();   // 创建工作簿对象
        String title = "学生信息";//文件的名的一部分
        for (int x = 1; x < 2; x++) {

            HSSFSheet sheet = workbook.createSheet(title + x);   // 创建工作表
            HashMap<String, Object> dataMap = new HashMap<String, Object>();
            List<HashMap<String, Object>> listMap = new ArrayList<HashMap<String, Object>>();

            for (int i = 0; i < listStudent.size(); i++) {
                Student st = listStudent.get(i);

                dataMap.put("id" + i, "id");
                dataMap.put("sid" + i, st.getId());
                dataMap.put("name" + i, st.getName());
                dataMap.put("gender" + i, st.getGender());
                dataMap.put("age" + i, st.getAge());
                dataMap.put("birthday" + i, st.getBirthday());
                dataMap.put("phone" + i, st.getPhoneNum());
                dataMap.put("group" + i, st.getGroup());

                listMap.add(dataMap);
            }


            String[] rowsName = new String[]{"序号", "学号", "姓名", "性别", "年龄", "生日", "手机号码", "组名"};
            List<Object[]> dataList = new ArrayList<Object[]>();
            Object[] objs = null;
            for (int i = 0; i < listMap.size(); i++) {
                HashMap<String, Object> data = listMap.get(i);
                objs = new Object[rowsName.length];
                objs[0] = data.get("id" + i);
                objs[1] = data.get("sid" + i);
                objs[2] = data.get("name" + i);
                objs[3] = data.get("gender" + i);
                objs[4] = data.get("age" + i);
                objs[5] = data.get("birthday" + i);
                objs[6] = data.get("phone" + i);
                objs[7] = data.get("group" + i);
                dataList.add(objs);
            }

            ExportExcel ex = new ExportExcel(title, rowsName, dataList);
            try {
                ex.export(workbook, sheet, x, titleInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }


        }


    }

/*
从本地excel导入数据
 */
    @Override
    public boolean importInfo(File file) {
        ArrayList<Student> list = new ArrayList<>();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        List<String[]> strings = null;
        try {
            strings = POIUtils.readExcel(file);

            for (int i = 1; i < strings.size(); i++) {
                String[] arr = strings.get(i);
                String id = arr[1];
                String name = arr[2];
                String gender = arr[3];
                LocalDate birthday = LocalDate.parse(arr[5], fmt);
                String phone = arr[6];
                int group = Integer.parseInt(arr[7]);
                Student student = new Student(id, name, gender, birthday, phone, group);
                list.add(student);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        listStudent = list;

        return true;
    }


}